package com.sfh.cpc.com.widget;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * 功能描述:
 * @date 2019/5/13
 */
public class SpaceItemLeftBottomDecoration extends RecyclerView.ItemDecoration  {
    private int space;

    public SpaceItemLeftBottomDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //不是第一个的格子都设一个左边和底部的间距
        outRect.left = space;
        outRect.bottom = space;

        int size = parent.getChildLayoutPosition(view);

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            //每行数量
            int spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            //由于每行第一个，所以第一个都是每行数量的倍数，把左边距设为0
            if (size % spanCount == 0) {
                outRect.left = 0;
            }
        } else {
            outRect.left = 0;
        }
    }
}
